package com.lagou.edu.service.impl;

import com.lagou.edu.annotation.MyAnnotation.Service;
import com.lagou.edu.annotation.MyAnnotation.Autowired;
import com.lagou.edu.annotation.MyAnnotation.Transactional1;
import com.lagou.edu.dao.AccountDao;
import com.lagou.edu.dao.impl.JdbcAccountDaoImpl;
import com.lagou.edu.pojo.Account;
import com.lagou.edu.service.TransferService;
import com.lagou.edu.utils.TransactionManager;

/**
 * @author 木笔
 */
@Service("transferService")
public class TransferServiceImpl implements TransferService {

    @Autowired
    private AccountDao accountDao;
    // private AccountDao accountDao = new JdbcAccountDaoImpl();

    // @Transactional1
    @Override
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {

//        try{
//            TransactionManager.getInstance().beginTransaction();

            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);

            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);

            accountDao.updateAccountByCardNo(to);
            // int c = 1/0;
            accountDao.updateAccountByCardNo(from);

//            TransactionManager.getInstance().commit();
//        }catch (Exception e) {
//            e.printStackTrace();
//            TransactionManager.getInstance().rollback();
//            throw e;
//        }

    }
}